/**
 *
 * The MIT License
 *
 * Copyright (c) 2008 Board of Trustees, Leland Stanford Jr. University. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.macdadi.multi.client.components;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.ui.*;

import java.util.ArrayList;

/**
 * @author Ben Suter <ben.suter@gmail.com>
 */
public class ProjectListingControls extends Composite implements HistoryListener
{
    public static final String CSS_CLASS = "macdadi-ProjectListingControls";
    public static final String CSS_CLASS_ACTIVE_LINK = "macdadi-ActiveLink";
    public static final String CSS_CLASS_LINK = "macdadi-Link";

    protected HTML indicator;
    protected WidgetDialog createNewDialog;

    protected ArrayList links = new ArrayList();
    protected LinkLabel createNew;
    protected Hyperlink linkAll, linkRecent, linkSearch, linkYours;

    public ProjectListingControls()
    {
        VerticalPanel vp = new VerticalPanel();

        indicator = new HTML("Please select one of these links to display a list of projects");

        createNewDialog = new WidgetDialog(new CreateProjectPanel(), "Please enter new project details");
        createNew = new LinkLabel("Create new");
        createNew.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                promptCreateNewProject();
            }
        });

        linkAll = new Hyperlink("All", "all");
        linkRecent = new Hyperlink("Recent", "recent");
        linkSearch = new Hyperlink("Search", "search");
        linkYours = new Hyperlink("Yours", "yours");

        links.add(linkAll);
        links.add(linkRecent);
        links.add(linkSearch);
        links.add(linkYours);

        /* Set CSS for all links, highlighting none */
        highlightLink(null);

        /* Arrange links left to right, followed by the message indicator.
         * Note: Originally the indicator was above the links, in a VerticalPanel.
         * Then I tried placing the indicator below the links. The best use of space
         * seems to be the current configuration, all on one line.
         */
        HorizontalPanel hp = new HorizontalPanel();

        hp.add(linkAll);
        hp.add(linkRecent);
        hp.add(linkYours);
        hp.add(linkSearch);
        hp.add(createNew);

        hp.add(indicator);
        
        hp.setStyleName("macdadi-LinkBar");
        vp.add(hp);

        initWidget(vp);
        setStyleName(CSS_CLASS);

        History.addHistoryListener(this);
    }

    /* API */
    public void promptCreateNewProject()
    {
        createNewDialog.show(createNew.getAbsoluteLeft(), createNew.getAbsoluteTop() + 20);
    }

    public void setMessage(String html)
    {
        indicator.setHTML(html);   
    }

    public void setEnabledYours(boolean isEnabled)
    {
        // TODO: find a way to show, but not be reponsive (and not participate in history tokens)
        linkYours.setVisible(isEnabled);
    }

    public void setEnabledCreateNew(boolean isEnabled)
    {
        // TODO: find a way to show, but not be reponsive (and not participate in history tokens)
        createNew.setVisible(isEnabled);
    }

    /**
     * Respond to changes in the history token, either in response to the user
     * clicking the back or forward browser buttons, or in response to
     * internal-hyperlink-induced state changes.
     *
     * @param historyToken the token representing the current history state
     */
    public void onHistoryChanged(String historyToken)
    {
        if ( "all".equals(historyToken) ) {
            highlightLink(linkAll);
        } else if ( "recent".equals(historyToken) ) {
            highlightLink(linkRecent);
        } else if ( "yours".equals(historyToken) ) {
            highlightLink(linkYours);
        } else if ( "search".equals(historyToken) ) {
            highlightLink(linkSearch);
        } else {
            highlightLink(null);
        }
    }

    protected void highlightLink(Widget link)
    {
        /* De-highlight all links */
        for (int i = 0; i < links.size(); i++) {
            ((Widget) links.get(i)).setStyleName(CSS_CLASS_LINK);
        }

        /* Highlight the specified link */
        if ( link != null )
            link.setStyleName(CSS_CLASS_ACTIVE_LINK);
    }
}
